package cn.Mmovie.dao.Impl;

import cn.Mmovie.bean.Cinema;
import cn.Mmovie.dao.CinemaDao;
import cn.Mmovie.util.DbUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

public class CinemaDaoImpl implements CinemaDao {
    @Override
    public Integer insert(Cinema cinema) {
        Integer result = -999;

        try {
            Connection conn = DbUtil.getConnection();
            String sql = "INSERT INTO cinema(name,location,capacity) " +
                    "VALUES(?,?,?)";
            PreparedStatement psmt = conn.prepareStatement(sql);
            psmt.setString(1, cinema.getName());
            psmt.setString(2, cinema.getLocation());
            psmt.setInt(3, cinema.getCapacity());
            result = psmt.executeUpdate();
            DbUtil.close(null, psmt, conn);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    @Override
    public Integer delete(Integer id) {
        Integer result = -999;

        try {
            //获取数据库连接对象
            Connection conn = DbUtil.getConnection();
            //创建预处理命令对象
            String sql = "DELETE FROM cinema WHERE id=?";
            PreparedStatement psmt = conn.prepareStatement(sql);
            //设置传入参数
            psmt.setInt(1, id);
            //执行操作，返回影响行数
            result = psmt.executeUpdate();
            //释放资源
            DbUtil.close(null, psmt, conn);
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return result;

    }

    @Override
    public Integer update(Cinema cinema) {
        Integer result = -999;

        try {
            Connection conn = DbUtil.getConnection();
            String sql = "UPDATE cinema SET name=?,location=?,capacity=? WHERE id=?";
            PreparedStatement psmt = conn.prepareStatement(sql);
            psmt.setString(1, cinema.getName());
            psmt.setString(2, cinema.getLocation());
            psmt.setInt(3, cinema.getCapacity());
            psmt.setInt(4,cinema.getId());
            result = psmt.executeUpdate();
            DbUtil.close(null, psmt, conn);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;

    }

    @Override
    public Cinema selectById(Integer id) {
        Cinema cinema = null;

        try {
            //获取数据库连接对象
            Connection conn = DbUtil.getConnection();
            //创建预处理命令对象
            String sql = "SELECT id,name,location,capacity FROM cinema WHERE id=?";
            PreparedStatement psmt = conn.prepareStatement(sql);
            //设置传入参数
            psmt.setInt(1, id);
            //执行操作，返回查询结果集
            ResultSet rs = psmt.executeQuery();
            if (rs.next()) {
                cinema = new Cinema();
                cinema.setId(rs.getInt("id"));
                cinema.setName(rs.getString("name"));
                cinema.setLocation(rs.getString("location"));
                cinema.setCapacity(rs.getInt("capacity"));
            }
            //释放资源
            DbUtil.close(rs, psmt, conn);
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return cinema;
    }

    @Override
    public List<Cinema> selectAll() {
        List<Cinema> cinemas = null;
        try {
            Connection conn = DbUtil.getConnection();
            String sql = "SELECT id,name,location,capacity FROM cinema";
            PreparedStatement psmt = conn.prepareStatement(sql);

            ResultSet rs = psmt.executeQuery();
            cinemas = new ArrayList<>();
            while (rs.next()) {
                Cinema cinema = new Cinema();
                cinema.setId(rs.getInt("id"));
                cinema.setName(rs.getString("name"));
                cinema.setLocation(rs.getString("location"));
                cinema.setCapacity(rs.getInt("capacity"));
                cinemas.add(cinema);
            }
            DbUtil.close(rs, psmt, conn);
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return cinemas;
    }

    @Override
    public Integer selectForRecordTotal() {
        Integer result = null;
        try {
            Connection conn = DbUtil.getConnection();
            String sql = "SELECT COUNT(*) FROM cinema ORDER BY id DESC";
            PreparedStatement psmt = conn.prepareStatement(sql);
            ResultSet rs = psmt.executeQuery();
            if (rs.next()) {
                result = rs.getInt(1);
            }
            DbUtil.close(null, psmt, conn);
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return result;
    }

    @Override
    public List<Cinema> selectForPageItems(Integer offset, Integer rows) {
        List<Cinema> cinemas = null;
        try {
            Connection conn = DbUtil.getConnection();
            String sql = "SELECT id,name,location,capacity FROM cinema ORDER BY id DESC LIMIT ?,?";
            PreparedStatement psmt = conn.prepareStatement(sql);
            psmt.setInt(1, offset);
            psmt.setInt(2, rows);

            ResultSet rs = psmt.executeQuery();
            cinemas = new ArrayList<>();
            while (rs.next()) {
                Cinema cinema = new Cinema();
                cinema.setId(rs.getInt("id"));
                cinema.setName(rs.getString("name"));
                cinema.setLocation(rs.getString("location"));
                cinema.setCapacity(rs.getInt("capacity"));
                cinemas.add(cinema);
            }
            DbUtil.close(rs, psmt, conn);
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return cinemas;
    }

    @Override
    public Cinema selectCinemaByName(String name) {
        Cinema cinema = null;
        try {
            Connection conn = DbUtil.getConnection();
            String sql = "SELECT * FROM cinema WHERE name=?";
            PreparedStatement psmt = conn.prepareStatement(sql);
            psmt.setString(1, name);
            ResultSet rs = psmt.executeQuery();
            if (rs.next()) {
                cinema = new Cinema();
                cinema.setId(rs.getInt("id"));
                cinema.setName(rs.getString("name"));
                cinema.setLocation(rs.getString("location"));
                cinema.setCapacity(rs.getInt("capacity"));
            }
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return cinema;
    }

}
